/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package swing.modelos;

import hibernate.GenericDAOHibernate;
import hibernate.exception.BussinessException;
import hibernate.util.HibernateUtil;
import java.io.Serializable;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
import org.hibernate.Transaction;

/**
 *
 * @author ariel
 */
public abstract class ModeloTabla<T, ID extends Serializable> extends AbstractTableModel {

    private ArrayList<T> listadoTabla;
    protected GenericDAOHibernate<T, ID> objetoLista = null;
    protected Transaction tx = null;
    private String[] columnas;

    public String[] getColumnas() {
        return columnas;
    }

    public void setColumnas(String[] columnas) {
        this.columnas = columnas;
    }

    public ArrayList<T> getListadoTabla() {
        return listadoTabla;
    }

    public void setListadoTabla(ArrayList<T> listadoTabla) {
        this.listadoTabla = listadoTabla;
    }

    public GenericDAOHibernate<T, ID> getObjetoLista() {
        return objetoLista;
    }

    public void setObjetoLista(GenericDAOHibernate<T, ID> objetoLista) {
        this.objetoLista = objetoLista;
    }

    public ModeloTabla(GenericDAOHibernate<T, ID> objeto) {
        try {
            this.objetoLista = objeto;
            this.objetoLista.setSession(HibernateUtil.getSessionFactory());
            this.tx = this.objetoLista.getSession().beginTransaction();
            listadoTabla = (ArrayList<T>) objetoLista.findAll();
            this.tx.commit();
        } catch (BussinessException ex) {
            this.tx.rollback();
        } finally {
            this.objetoLista.getSession().close();
        }
    }

    public T getRow(int index) {
        return listadoTabla.get(index);
    }

    @Override
    public int getColumnCount() {
        return columnas.length;
    }

    @Override
    public int getRowCount() {
        return listadoTabla.size();
    }

    @Override
    public String getColumnName(int column) {
        return columnas[column];
    }

    public void deleteRow(int selectedRow) {
        try {
            this.tx = this.objetoLista.getSession().beginTransaction();
            T get = (T) this.listadoTabla.get(selectedRow);
            objetoLista.delete(get);
            rellenaTabla();
            this.tx.commit();
        } catch (BussinessException ex) {
            this.tx.rollback();
        } finally {
            this.objetoLista.getSession().close();

        }
    }

    public void addRow(T objeto) {
        try {
            this.tx = this.objetoLista.getSession().beginTransaction();
            objetoLista.saveOrUpdate(objeto);
            rellenaTabla();
            fireTableDataChanged();
            this.tx.commit();
        } catch (BussinessException ex) {
            this.tx.rollback();
        } finally {
            this.objetoLista.getSession().close();
        }
    }

    public void rellenaTabla() throws BussinessException {
        this.listadoTabla = (ArrayList<T>) objetoLista.findAll();
    }
    /**
    public void buscar(ID cadena) throws BussinessException{
        this.listadoTabla = new ArrayList();
        this.listadoTabla = (ArrayList<T>) objetoLista.buscar(cadena);
        this.fireTableDataChanged();
    }
    * **/
}
